'use strict';

module.exports = appInfo => {
  const config = exports = {};
  // config.env = 'local'; // 环境配置，会加载config.env.js配置文件

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_key';

  config.appInfo = appInfo;

  config.logger = {
    dir: appInfo.baseDir + '/logs/' + appInfo.name, //  默认放在 ${appInfo.root}/logs/${appInfo.name} root根据环境有可能在/home|/root目录下
  };

  config.proxy = true;

  config.cluster = {
    listen: {
      port: 7001,
      hostname: '0.0.0.0', // '127.0.0.1',
    },
  };

  // 公共配置
  config.common = {
    pageSize: 10, // 数据分页
    db_op_record: true, // 数据库操作记录
  };

  config.siteFile = { // 设置网站角标
    '/favicon.ico': '/public/favicon.ico',
  };

  // 视图渲染插件配置
  config.view = {
    defaultViewEngine: 'nunjucks',
    mapping: {
      '.tpl': 'nunjucks',
    },
  };

  // mysql插件配置
  config.mysql = {
    client: {
      host: 'localhost',
      port: '3306',
      user: 'root',
      password: 'root',
      database: 'egg_main',
    },
  };
  // mysql的orm映射插件配置
  config.sequelize = {
    datasources: [
      {
        delegate: 'model', // 加载所有的模型到 app.model and ctx.model
        baseDir: 'model', // 要加载的模型目录`app/model/*.js`
        database: config.mysql.client.database,
        dialect: 'mysql',
        host: config.mysql.client.host,
        port: config.mysql.client.port,
        username: config.mysql.client.user,
        password: config.mysql.client.password,
        timezone: 'Asia/Shanghai', // 时间差
        benchmark: true,
        define: {
          freezeTableName: true, // 禁止数据读写时给表赋别名(as)
          underscored: true,
          timestamps: false, // 为模型添加createdAt和updatedAt字段
          raw: true,
        },
      },
      {
        delegate: 'logModel', // 加载所有的模型到 app.logModel and ctx.logModel
        baseDir: 'model_log', // 要加载的模型目录 `app/admin_model/*.js`
        database: 'egg_log',
        dialect: 'mysql',
        host: config.mysql.client.host,
        port: config.mysql.client.port,
        username: config.mysql.client.user, // 'log',
        password: config.mysql.client.password,
        timezone: 'Asia/Shanghai',
        benchmark: true,
        define: {
          freezeTableName: true, // 禁止数据读写时给表赋别名(as)
          underscored: true,
          timestamps: false, // 为模型添加createdAt和updatedAt字段
          raw: true,
        },
      },
    ],
  };

  // redis 插件配置
  config.redis = {
    client: {
      port: 6379, // Redis port
      host: '127.0.0.1', // Redis host
      password: '123456',
      db: 0,
    },
  };

  // 指定路径关闭csrf
  config.security = {
    csrf: {
      ignore: [ '/admin/set_status', '/admin/save_upload', '/admin/upload', '/admin/tools/qrcode_text', '/admin/tools/qrcode', '/ueditor' ],
      xframe: {},
    },
  };

  config.multipart = {
    fileSize: '50mb',
    fileExtensions: [ '.7z' ], // 添加上传允许的扩展名
  };

  // add your config here
  config.middleware = [
    'robot', 'adminauth', 'navigate', 'checklogin', 'siteconfig',
  ];
  // robot's configurations
  config.robot = {
    enable: true,
    ignore: /^\/home/, // 关闭的url
    ua: [
      /Baiduspider/i,
    ],
  };
  // admin权限验证配置
  config.adminauth = {
    enable: true, // 是否开启权限验证
    match: /^\/admin/, // 开启的url
    allow_active: [
      /^\/admin\/index\/(index|login|logout)/, /^\/admin\/(captcha|upload|save_upload)/, // 跳过的规则
    ],
  };

  // 登录验证配置
  config.checklogin = {
    enable: true,
    match: /^\/(admin|member)/, // 开启的url
    allow_active: {
      admin: [ /^\/admin\/index\/(login|logout)/, /^\/admin\/captcha/ ],
      member: [ /^\/member\/index\/(login|logout)/, /^\/member\/captcha/ ],
    },
    jump_login_url: {
      admin: '/admin/index/login',
      member: '/member/index/login',
    },
  };
  // admin主内容导航菜单配置
  config.navigate = {
    enable: true,
    match: /^\/admin/, // 开启的url
  };

  config.siteconfig = {
    enable: true,
    match: /^\/admin/,
  };

  // 短信模板
  config.sms_templete = [
    '【企业名称】验证码是#code#，您正在注册成为新用户，感谢您的支持！',
    '【企业名称】验证码是#code#，您正在绑定手机号，感谢您的支持！',
    '【企业名称】验证码是#code#，您正在修改密码，感谢您的支持！',
  ];

  // io
  exports.io = {
    init: { }, // passed to engine.io
    namespace: {
      '/': {
        connectionMiddleware: [ 'connection' ],
        packetMiddleware: [],
      },
      '/example': {
        connectionMiddleware: [],
        packetMiddleware: [],
      },
    },
  };

  return config;
};
